import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_todo_ohos/common/index.dart';
import 'package:getx_scaffold_ohos/getx_scaffold_ohos.dart';

import 'routes/index.dart';

void main() async{
  await ScreenUtil.ensureScreenSize();
  await init(
    isDebug: kDebugMode,
    logTag: '蜜蜂TODO',
    supportedLocales: TranslationLibrary.supportedLocales
  );
  /// 网络配置
  await HttpConfig.init();
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with WidgetsBindingObserver{

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeMetrics() {
    super.didChangeMetrics();
    final aspectRatio = MediaQuery.of(context).size.aspectRatio;
    if (kDebugMode){
      print('aspectRatio: $aspectRatio');
    }
    refreshAppui();
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (_, constraints){
        final Size designSize = _selectDesignSize(constraints.maxWidth);
        print("当前屏幕尺寸：${constraints.maxWidth}");
        print("当前设计稿的尺寸: ${designSize.width} * ${designSize.height}");
        return RefreshConfiguration(
          headerBuilder: () => const ClassicHeader(),        // 配置默认头部指示器,假如你每个页面的头部指示器都一样的话,你需要设置这个
          footerBuilder:  () => const ClassicFooter(),        // 配置默认底部指示器
          springDescription:const SpringDescription(stiffness: 170, damping: 16, mass: 1.9),
          enableLoadingWhenFailed : true, //在加载失败的状态下,用户仍然可以通过手势上拉来触发加载更多
          hideFooterWhenNotFull: false, // Viewport不满一屏时,禁用上拉加载更多功能
          enableBallisticLoad: true, // 可以通过惯性滑动触发加载更多
          child: GetxApp(
            // 设计稿尺寸 单位：dp
            designSize: designSize,
            minTextAdapt: true,
            splitScreenMode: true,
            // Getx Log
            enableLog: kDebugMode,
            // 默认的跳转动画
            defaultTransition: Transition.rightToLeft,
            // 主题模式
            themeMode: GlobalService.to.themeMode,
            // 主题
            theme: AppTheme.light,
            // Dark主题
            darkTheme: AppTheme.dark,
            // 国际化配置
            locale: GlobalService.to.locale,
            translations: TranslationLibrary(),
            fallbackLocale: TranslationLibrary.fallbackLocale,
            supportedLocales: TranslationLibrary.supportedLocales,
            localizationsDelegates: TranslationLibrary.localizationsDelegates,
            // AppTitle
            title: '蜜蜂TODO',
            getPages: AppPages.pages,
            initialRoute: RouteKey.root,
            // Builder
            builder: (context, widget) {
              // do something....
              return widget!;
            },
          ),
        );
      },
    );
  }

  /// 选择设计稿尺寸
  ///
  Size _selectDesignSize(double screenWidth) {
    if(PlatformUtil.isMacOS) return const Size(1440, 900); // mac
    if(PlatformUtil.isWindows) return const Size(1920, 1080);// windows
    if(screenWidth > 1000) return const Size(1440, 900);// 桌面端通用
    if (screenWidth > 600) return const Size(768, 1024);// 平板通用
    return const Size(375, 812);// 手机
  }
}

